package Observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class WordAndNumObserver implements Observer {
    private List<String> wordFrequencyList = new ArrayList<>();
    @Override
    public void update(Text t) {
        List<String> text = t.getText();
        Map<String, Long> frequencyMap = text.stream()
            .collect(Collectors.groupingBy(word -> word, Collectors.counting()));
        List<Map.Entry<String, Long>> sortedEntries = frequencyMap.entrySet().stream()
            .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
            .collect(Collectors.toList());
        for (Map.Entry<String, Long> entry : sortedEntries) {
            wordFrequencyList.add(entry.getKey() + ":" + entry.getValue());
        }
    }
}
